<?php

class BannerController extends BackEndController {

    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }


    public function accessRules() {
        return array(
            array(
                'allow',
                'actions' => array('login', 'logout'),
                'users' => array('*'),
            ),
            array(
                'allow',
                'actions' => array('index', 'view', 'create', 'update', 'admin', 'delete'),
                'users' => array('@'),
                'expression' => 'Yii::app()->user->isAdmin() == "true"',
            ),
            array('deny', // deny all users
                'users' => array('*'),
            )
        );
    }

    public function actionView($id) {
        $this->render('view', array(
            'model' => $this->loadModel($id),
        ));
    }


    public function actionCreate() {
        $model = new Banner;

        if (isset($_POST['Banner'])) {
            $model->attributes = $_POST['Banner'];
            $file = CUploadedFile::getInstance($model, 'image');
            if ($file != null) {
                $file = CUploadedFile::getInstance($model, 'image');
                $extension = $file->getExtensionName();
                $fileNameVideo = date('YmdHis') . '_' . rand(10000, 99999) . '.' . $extension;
                $file->saveAs(dirname(Yii::getPathOfAlias('application')) . '/files/banners/' . $fileNameVideo);
                $model->picture = TreTodayUrl::home() . '/files/banners/' . $fileNameVideo;

                if ($model->save()) {
                    $this->redirect(array('index'));
                }
            } else {
                $model->addError('picture', 'Vui lòng chọn hình ảnh');
            }
        }

        $this->render('create', array(
            'model' => $model,
        ));
    }

    public function actionUpdate($id) {
        $model = $this->loadModel($id);
        if (isset($_POST['Banner'])) {
            $model->attributes = $_POST['Banner'];

            $file = CUploadedFile::getInstance($model, 'image');
            if ($file != null) {
                $file = CUploadedFile::getInstance($model, 'image');
                $extension = $file->getExtensionName();
                $fileNameVideo = date('YmdHis') . '_' . rand(10000, 99999) . '.' . $extension;
                $file->saveAs(dirname(Yii::getPathOfAlias('application')) . '/files/banners/' . $fileNameVideo);
                $model->picture = TreTodayUrl::home() . '/files/banners/' . $fileNameVideo;

                if ($model->save()) {
                    $this->redirect(array('index'));
                }
            }

            if ($model->save())
                $this->redirect(array('index'));
        }

        $this->render('update', array(
            'model' => $model,
        ));
    }

    public function actionDelete($id) {
        $this->loadModel($id)->delete();

        // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
        if (!isset($_GET['ajax']))
            $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));
    }

    public function actionIndex() {
        $model = new Banner('search');
        $model->unsetAttributes(); // clear any default values
        if (isset($_GET['Banner']))
            $model->attributes = $_GET['Banner'];

        $this->render('admin', array(
            'model' => $model,
        ));
    }

    public function loadModel($id) {
        $model = Banner::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

}
